package com.koron.order.experiment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.experiment.bean.po.ExperimentEquipmentPo;
import com.koron.order.experiment.bean.po.ExperimentParamsPo;
import com.koron.order.experiment.bean.po.ParamType;
import com.koron.order.experiment.bean.vo.ExperimentEquipmentVo;
import com.koron.order.experiment.bean.vo.ExperimentItemVo;
import com.koron.order.experiment.mapper.ExperimentEquipmentMapper;
import com.koron.order.experiment.service.ExperimentEquipmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * (ExperimentEquipment)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-08 16:42:58
 */
@Service("experimentEquipmentService")
public class ExperimentEquipmentServiceImpl implements ExperimentEquipmentService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, ExperimentEquipmentPo experimentEquipment) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        if (StringUtils.isEmpty(experimentEquipment.getId())) {
            experimentEquipment.setId(CodeTools.getCode32());
        }

        return mapper.insertOrUpdate(experimentEquipment);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<ExperimentEquipmentPo> experimentEquipmentList) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        if (CollectionUtils.isEmpty(experimentEquipmentList)) {
            return 0;
        }
        experimentEquipmentList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        return mapper.insertOrUpdateBatch(experimentEquipmentList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, ExperimentEquipmentPo experimentEquipment) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        if (StringUtils.isEmpty(experimentEquipment.getId())) {
            experimentEquipment.setId(CodeTools.getCode32());
        }

        return mapper.insert(experimentEquipment);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<ExperimentEquipmentPo> experimentEquipments) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        if (CollectionUtils.isEmpty(experimentEquipments)) {
            return 0;
        }
        String experimentId = experimentEquipments.get(0).getExperimentId();
        deleteByOrderId(factory, experimentId);
        experimentEquipments.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        return mapper.insertBatch(experimentEquipments);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String experimentId) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);
        return mapper.deleteByOrderId(experimentId);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, ExperimentEquipmentPo experimentEquipment) {
        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);
        return mapper.update(experimentEquipment);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public ExperimentEquipmentPo queryById(SessionFactory factory, String id) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ExperimentEquipmentPo> queryList(SessionFactory factory, ExperimentEquipmentPo experimentEquipment) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        List<ExperimentEquipmentPo> list = mapper.queryList(experimentEquipment);

        return list;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<ExperimentEquipmentVo> queryByOrderId(SessionFactory factory, String orderId) {

        return queryByBusinessId(factory, orderId, null);
    }

    @Override
    @TaskAnnotation("queryByExperimentEquipmentId")
    public List<ExperimentEquipmentVo> queryByExperimentEquipmentId(SessionFactory factory, String experimentEquipmentId) {

        return queryByBusinessId(factory, null, experimentEquipmentId);
    }

    public List<ExperimentEquipmentVo> queryByBusinessId(SessionFactory factory, String orderId, String experimentEquipId) {

        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        List<ExperimentEquipmentVo> list = mapper.queryByBusinessId(orderId, experimentEquipId);

        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        for (ExperimentEquipmentVo experimentEquipmentVo : list) {
            List<ExperimentItemVo> itemVos = experimentEquipmentVo.getItemVos();
            if (CollectionUtils.isNotEmpty(itemVos)) {
                Map<String, ExperimentItemVo> map = new HashMap<>();
                for (ExperimentItemVo itemVo : itemVos) {
                    String id = itemVo.getId();
                    String parentId = itemVo.getParentId();
                    itemVo.setExperimentItemVoList(new ArrayList<>());
                    if (StringUtils.isEmpty(parentId)) {
                        map.put(id, itemVo);
                    }
                }

                Iterator<ExperimentItemVo> iterator = itemVos.iterator();
                //改变层级,将外层转化为内层
                while (iterator.hasNext()) {
                    ExperimentItemVo itemVo = iterator.next();
                    String id = itemVo.getId();
                    String parentId = itemVo.getParentId();
                    if (StringUtils.isNotEmpty(parentId)) {
                        ExperimentItemVo experimentItemVo = map.get(parentId);
                        if (!Objects.isNull(experimentItemVo)) {
                            //找到了父节点,应该从原来列表删除
                            iterator.remove();
                            List<ExperimentItemVo> experimentItemVoList = experimentItemVo.getExperimentItemVoList();
                            experimentItemVoList.add(itemVo);
                        }
                    }
                }

            }
        }

        //处理一下list
        list.forEach(p -> {
            List<ExperimentParamsPo> envParams = p.getEnvParams();
            if (!CollectionUtils.isEmpty(envParams)) {

                List<ExperimentParamsPo> envList = envParams.stream().filter(s -> ParamType.ENVIRONMENT.equals(s.getType())).collect(Collectors.toList());
                List<ExperimentParamsPo> deviceList = envParams.stream().filter(s -> ParamType.EQUIPMENT.equals(s.getType())).collect(Collectors.toList());

                p.setEnvParams(envList);
                p.setDeviceParams(deviceList);

            }
        });

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, ExperimentEquipmentPo experimentEquipment, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());
        ExperimentEquipmentMapper mapper = factory.getMapper(ExperimentEquipmentMapper.class);

        List<ExperimentEquipmentPo> list = mapper.queryList(experimentEquipment);

        return new PageInfo(list);
    }

}